package org.urbansoft.icl.record;

import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.urbansoft.icl.field.Field;
import org.urbansoft.icl.field.GenericANField;
import org.urbansoft.icl.field.GenericBlankField;
import org.urbansoft.icl.field.GenericNField;
import org.urbansoft.icl.field.Position;
import org.urbansoft.icl.field.Usage;
import org.urbansoft.icl.field.ValidationCriteria;
import org.urbansoft.icl.field.validation.RecordTypeValidator;

/**
 * The Class FileControlRecord.
 */
public class FileControlRecord extends Record {

	public Field recordType;
	public Field cashLetterCount;
	public Field totalRecordsCount;
	public Field totalItemsCount;
	public Field fileTotalAmount;
	public Field immediateOriginContactName;
	public Field immediateOriginContactPhone;
	public Field reserved;
	private static Logger log = Logger.getLogger(FileControlRecord.class);

	/**
	 * Instantiates a new file control record.
	 */
	public FileControlRecord() {
		fields = new ArrayList<Field>();
		recordName = "File Control Record";
		recordTypeNumber = "90";
		initFields();
	}

	/**
	 * Instantiates a new file control record.
	 */
	public FileControlRecord(String read) {
		// log.info("New FileControlRecord for READ");
		fields = new ArrayList<Field>();
		recordName = "File Control Record";
		recordTypeNumber = "90";
		initFields2();
	}

	/**
	 * Instantiates a new file control record.
	 */
	public FileControlRecord(int read) {
		// log.info("New FileControlRecord for READ");
		fields = new ArrayList<Field>();
		recordName = "File Control Record";
		recordTypeNumber = "90";
		initFields3();
	}

	/**
	 * Inits the fields.
	 */
	private void initFields2() {
		try {
			/* check detail record */
			recordType = new GenericANField(new Position(1, 2), "Record Type");

			/* cash letter count */
			cashLetterCount = new GenericANField(new Position(3, 8),
					"Cash Letter Count");

			/* total records count */
			totalRecordsCount = new GenericANField(new Position(9, 16),
					"Total Records Count");

			/* total items count */
			totalItemsCount = new GenericANField(new Position(17, 24),
					"Total Items Count");

			/* file total amount */
			fileTotalAmount = new GenericANField(new Position(25, 45),
					"File Total Amount");

			/* immediate origin name */
			immediateOriginContactName = new GenericANField(
					new Position(46, 59), "Immediate Origin Contact Name");

			/* immediate origin phone */
			immediateOriginContactPhone = new GenericANField(new Position(60,
					69), "Immediate Origin Contact Phone Number");

			/* reserved */
			reserved = new GenericANField(new Position(70, 90), "Reserved");

			fields.add(recordType);
			fields.add(cashLetterCount);
			fields.add(totalRecordsCount);
			fields.add(totalItemsCount);
			fields.add(fileTotalAmount);
			fields.add(immediateOriginContactName);
			fields.add(immediateOriginContactPhone);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	private void initFields3() {
		try {
			/* check detail record */
			recordType = new GenericANField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "99", "Record Type");

			/* cash letter count */
			cashLetterCount = new GenericANField(new Position(3, 8),
					"Cash Letter Count");
			cashLetterCount.setFieldData("1");

			/* total records count */
			totalRecordsCount = new GenericANField(new Position(9, 16),
					"Total Records Count");
			totalRecordsCount.setFieldData("1");

			/* total items count */
			totalItemsCount = new GenericANField(new Position(17, 24),
					"Total Items Count");
			totalItemsCount.setFieldData("1");

			/* file total amount */
			fileTotalAmount = new GenericANField(new Position(25, 45),
					"File Total Amount");
			fileTotalAmount.setFieldData("1");

			/* immediate origin name */
			immediateOriginContactName = new GenericANField(
					new Position(46, 59), "Immediate Origin Contact Name");
			immediateOriginContactName.setFieldData("");

			/* immediate origin phone */
			immediateOriginContactPhone = new GenericANField(new Position(60,
					69), "Immediate Origin Contact Phone Number");
			immediateOriginContactPhone.setFieldData("");

			/* reserved */
			reserved = new GenericANField(new Position(70, 90), "Reserved");
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(cashLetterCount);
			fields.add(totalRecordsCount);
			fields.add(totalItemsCount);
			fields.add(fileTotalAmount);
			fields.add(immediateOriginContactName);
			fields.add(immediateOriginContactPhone);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}

	/**
	 * Inits the fields.
	 */
	private void initFields() {
		try {
			/* check detail record */
			recordType = new GenericNField(new Position(1, 2),
					RecordTypeValidator.INSTANCE, "99", "Record Type");
			recordType.setValidationCriteria(ValidationCriteria.REQUIRED);

			/* cash letter count */
			cashLetterCount = new GenericNField(new Position(3, 8),
					"Cash Letter Count");

			/* total records count */
			totalRecordsCount = new GenericNField(new Position(9, 16),
					"Total Records Count");

			/* total items count */
			totalItemsCount = new GenericNField(new Position(17, 24),
					"Total Items Count");

			/* file total amount */
			fileTotalAmount = new GenericNField(new Position(25, 40),
					"File Total Amount");

			/* immediate origin name */
			immediateOriginContactName = new GenericANField(
					new Position(41, 54), "Immediate Origin Contact Name");
			immediateOriginContactName.setUsage(Usage.CONDITIONAL);

			/* immediate origin phone */
			immediateOriginContactPhone = new GenericANField(new Position(55,
					64), "Immediate Origin Contact Phone Number");
			immediateOriginContactPhone.setUsage(Usage.CONDITIONAL);

			/* reserved */
			reserved = new GenericBlankField(new Position(65, 80), "Reserved");
			reserved.setFieldData("");

			fields.add(recordType);
			fields.add(cashLetterCount);
			fields.add(totalRecordsCount);
			fields.add(totalItemsCount);
			fields.add(fileTotalAmount);
			fields.add(immediateOriginContactName);
			fields.add(immediateOriginContactPhone);
			fields.add(reserved);

		} catch (Exception e) {
			log.fatal(e.getMessage(), e);
		}
	}
}
